import StageScene from "./StageScene"
import Bullet from "./Bullet"

export default class Turret extends Laya.Script3D {
  // 武器明
  private weaponNameArr = [
    'Turret', 'Turret_up', 'MissileLauncher', 'MissileLauncher_up', 'LaserBeamer', 'LaserBeamer_up',
    1000, 600, 900, 540, 0, 0
  ]
  //攻击频率
  private attatcTime
  private attackTime_max
  private enemyArr: Array<any>
  private weapon: Laya.Sprite3D
  private head: Laya.Sprite3D
  private bulletStart: Laya.Sprite3D
  private static reduceTime = 16
  private isLaser: boolean
  private weaponType: number
  private bullets: Array<Laya.Sprite3D>
  onEnable() {
    this.bullets = new Array(3)
    this.weapon = this.owner as Laya.Sprite3D
    this.head = this.owner.getChildByName('Head') as Laya.Sprite3D
    this.bulletStart = this.head.getChildAt(0).getChildByName('BulletPos') as Laya.Sprite3D
    this.enemyArr = new Array()
    this.weaponNameArr.some((item, index) => {
      if (item === this.weapon.name) {
        this.attatcTime = this.weaponNameArr[index + 6]
        if (index < 2) this.weaponType = 0
        else if (index < 4) this.weaponType = 1
        else this.weaponType = 2
        return true
      }
    })
    this.attackTime_max = this.attatcTime
    this.isLaser = this.attatcTime == 0
  }
  onTriggerEnter(collider: Laya.PhysicsCollider) {
    this.enemyArr.push(collider.owner)
    console.log(collider.owner.name + '进来了， 现在数量有' + this.enemyArr.length)
  }

  onTriggerExit(collider: Laya.PhysicsCollider) {
    this.enemyArr.some((item, index) => {
      if (item === collider.owner) {
        this.enemyArr.splice(index, 1)
        return true
      }
    })
    console.log(collider.owner.name + '离开了， 现在数量有' + this.enemyArr.length)
  }

  onUpdate() {
    this.attatcTime -= Turret.reduceTime
    let enemy = this.enemyArr[0] as Laya.Sprite3D
    if (!enemy) return
    if (enemy.destroyed) {
      // 清理数组中被消灭的敌人
      for (let i = 0; i < this.enemyArr.length;) {
        let enemy = this.enemyArr[i]
        if (enemy.destroyed) {
          this.enemyArr.splice(0, 1)
        } else i++
      }
    }
    enemy = this.enemyArr[0]
    // 判断是否还有敌人
    if (enemy) {
      // 炮口指向敌人
      let position = enemy.transform.position
      var pos = new Laya.Vector3(position.x, this.head.transform.position.y, position.z)
      this.head.transform.lookAt(pos, new Laya.Vector3(0, 1, 0))
      this.head.transform.rotate(new Laya.Vector3(0, 180, 0), false, false)
      if (this.attatcTime <= 0 && !this.isLaser) {
        // 攻击敌人
        this.attack(enemy)
      }
    }
  }
  
  private attack(enemy) {
    let bulletPrefbs = this.bullets[this.weaponType]
    if (!bulletPrefbs) {
      // 创建预制体
      let path = 'res/atlas/Models/Conventional/Bullet.lh'
      if (this.weaponType == 1) path = 'res/atlas/Models/Conventional/Missile.lh'
      let bullet = Laya.loader.getRes(path)
      let rig: Laya.Rigidbody3D
      if (this.weaponType == 0) rig = bullet.getComponent(Laya.Rigidbody3D) as Laya.Rigidbody3D
      else  if (this.weaponType == 1) {
        let child =  bullet.getChildAt(0) as Laya.Sprite3D
        // child.transform.rotate(new Laya.Vector3(0, 180, 0), false, false)
        rig = child.getComponent(Laya.Rigidbody3D)
      }
      rig.canCollideWith = Laya.Physics3DUtils.COLLISIONFILTERGROUP_CUSTOMFILTER8
      rig.collisionGroup = Laya.Physics3DUtils.COLLISIONFILTERGROUP_CUSTOMFILTER6
      bullet.addComponent(Bullet)
      this.bullets[this.weaponType] = bullet
      bulletPrefbs = this.bullets[this.weaponType] = bullet
    }
    // 创建子弹
    let bullet = Laya.Sprite3D.instantiate(bulletPrefbs, StageScene.instance.stageScene, false, this.bulletStart.transform.position)
    // bullet.transform.position = this.bulletStart.transform.position
    let bulletScript = bullet.getComponent(Bullet) as Bullet
   bulletScript.attack(enemy, this.weaponType)
    this.attatcTime = this.attackTime_max
  }
}